﻿using System.ServiceModel;
using System;
using System.Diagnostics;

namespace NhJump.Persistence.Context.WcfBinding
{
    public class WcfUnitOfWorkExtension : IExtension<InstanceContext>
    {
        private readonly UnitOfWorkFactory _factory;

        public UnitOfWork UnitOfWork { get; set; }

        public WcfUnitOfWorkExtension(UnitOfWorkFactory factory)
        {
            _factory = factory;
        }

        public void Attach(InstanceContext owner)
        {
            // this effectively forms the bind
            UnitOfWork = _factory.BuildAutoBoundUnitOfWork();
        }

        public void Detach(InstanceContext owner)
        {
            UnitOfWork.Commit();
        }
    }
}